package com.hxx.config;

import com.hxx.ai.BookingAssistant;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static dev.langchain4j.agent.tool.JsonSchemaProperty.description;
import static dev.langchain4j.agent.tool.JsonSchemaProperty.type;

@Configuration
public class LLMConfig {

    @Autowired
    private BookingConfig bookingConfig;

    //模型
    @Bean
    public StreamingChatLanguageModel streamingChatLanguageModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey("sk-1f935c2000000000000")
                .modelName("qwen2.5-7b-instruct-1m")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean
    public BookingAssistant bookingAssistant(StreamingChatLanguageModel streamingChatLanguageModel) {
        //获取机票信息
        ToolSpecification bookingSpecification = ToolSpecification.builder()
                .name("bookingFunction")
                .description("获取机票预定信息")
                .addParameter("bookingNumber", type("string"), description("预订号码"))
                .addParameter("name", type("string"), description("姓名"))
                .build();

        ToolExecutor bookingExecutor =(toolExecutionRequest, memoryId)-> {
            return toolExecutionRequest.arguments();
        };
        //取消机票信息
        ToolSpecification cancelSpecification = ToolSpecification.builder()
                .name("cancelBooking")
                .description("取消机票预定信息")
                .addParameter("bookingNumber", type("string"), description("预订号码"))
                .addParameter("name", type("string"), description("姓名"))
                .build();
        ToolExecutor cancelExecutor =(toolExecutionRequest, memoryId)-> {
            return toolExecutionRequest.arguments();
        };

        //修改机票信息
        ToolSpecification changeSpecification = ToolSpecification.builder()
                .name("changeBooking")
                .description("修改机票预定信息")
                .addParameter("bookingNumber", type("string"), description("预订号码"))
                .addParameter("name", type("string"), description("姓名"))
                .addParameter("date", type("string"), description("日期"))
                .addParameter("from", type("string"), description("出发地"))
                .addParameter("to", type("string"), description("目的地"))
               .build();
        ToolExecutor changeExecutor =(toolExecutionRequest, memoryId)-> {
            return toolExecutionRequest.arguments();
        };

        return AiServices.builder(BookingAssistant.class)
                .streamingChatLanguageModel(streamingChatLanguageModel)
                .chatMemoryProvider(cmp-> MessageWindowChatMemory.withMaxMessages(100))
                .tools(bookingSpecification, bookingExecutor)
                .tools(cancelSpecification, cancelExecutor)
                .tools(changeSpecification, changeExecutor)
                .tools(bookingConfig)
                .build();
    }
}































